package com.ewing.busi.customer.aop;

import java.lang.reflect.Method;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import com.ewing.common.constants.AjaxRespCode;
import com.ewing.common.utils.SystemPropertyUtils;
import com.ewing.core.app.action.base.BaseAction;

/**
 * 
 * 
 * @author tansonlam
 * @createDate 2016年2月16日
 * 
 */
@Aspect
public class CustomerValidateLoginAdvice {

  private static Logger logger = Logger.getLogger(CustomerValidateLoginAdvice.class);

  @Around("execution(* com.ewing.busi.*.action.*.*(..))  && @annotation(filter)")
  public Object aroundMethod(ProceedingJoinPoint pjd, CustomerLoginFilter filter) throws Throwable {
    if (!SystemPropertyUtils.needAuth() || !(pjd.getTarget() instanceof BaseAction)
        || filter == null)
      return pjd.proceed();

    Method getLoginUserIdMethod = pjd.getTarget().getClass().getMethod("getLoginUserId");

    try {
      Integer userId = (Integer) getLoginUserIdMethod.invoke(pjd.getTarget());
      logger.info(userId);
    } catch (Exception e) {
      Method outFailResultMethod =
          pjd.getTarget().getClass().getMethod("outFailResult", AjaxRespCode.class);
      outFailResultMethod.invoke(pjd.getTarget(), AjaxRespCode.CUSTOMER_NOLOGIN);
    }

    return pjd.proceed();
  }
}
